<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="danger" size="small" plain @click="handleExport"
          >数据导出
        </el-button>
      </template>
      <template slot="deptId" slot-scope="{ row }">
        <div>{{ row.deptName }}</div>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { getList, getDetail, add, update, remove } from "@/api/wages/wages";
import { mapGetters } from "vuex";
import { getDeptTree } from "@/api/system/dept";
import { getToken } from "@/util/auth";
export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        height: "auto",
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        menu: false,
        index: true,
        cellBtn: true,
        viewBtn: true,
        selection: true,
        dialogClickModal: false,
        column: [
          {
            label: "会计期间",
            prop: "accountPeriod",
            cell: true,
            type: "month",
            search: true,
            format: "yyyy-MM",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            rules: [
              {
                required: true,
                message: "请输入会计期间",
                trigger: "blur",
              },
            ],
          },
          {
            label: "所属组织",
            prop: "deptId",
            dicData: [],
            type: "tree",
            cell: true,
            addDisabled: false,
            props: {
              label: "title",
            },
            width: 300,
            slot: true,
            rules: [
              {
                required: true,
                message: "请输入所属组织id",
                trigger: "blur",
              },
            ],
          },
          // {
          //   label: "所属组织",
          //   prop: "deptName",
          //   rules: [{
          //     required: true,
          //     message: "请输入所属组织",
          //     trigger: "blur"
          //   }]
          // },
          {
            label: "姓名",
            prop: "name",
            cell: true,
            rules: [
              {
                required: true,
                message: "请输入姓名",
                trigger: "blur",
              },
            ],
          },
          {
            label: "身份证",
            prop: "idCard",
            cell: true,
            width: 200,
            rules: [
              {
                required: true,
                message: "请输入身份证",
                trigger: "blur",
              },
            ],
          },
          {
            label: "银行卡号",
            prop: "bankNo",
            width: 200,
            cell: true,
            rules: [
              {
                required: true,
                message: "请输入银行卡号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "基本工资合计",
            prop: "basicWages",
            cell: true,
            rules: [
              {
                required: true,
                message: "请输入基本工资合计",
                trigger: "blur",
              },
            ],
          },
          {
            label: "补贴合计",
            prop: "allowanceTotal",
            cell: true,
            rules: [
              {
                required: true,
                message: "请输入补贴合计",
                trigger: "blur",
              },
            ],
          },
          {
            label: "绩效奖金",
            prop: "achievementBonus",
            cell: true,
            rules: [
              {
                required: true,
                message: "请输入绩效奖金",
                trigger: "blur",
              },
            ],
          },
          {
            label: "补发",
            prop: "reissue",
            cell: true,
            rules: [
              {
                required: true,
                message: "请输入补发",
                trigger: "blur",
              },
            ],
          },
          {
            label: "应发合计",
            prop: "payableTotal",
            cell: true,
            rules: [
              {
                required: true,
                message: "请输入应发合计",
                trigger: "blur",
              },
            ],
          },
          {
            label: "扣除合计",
            prop: "deductingTotal",
            cell: true,
            rules: [
              {
                required: true,
                message: "请输入扣除合计",
                trigger: "blur",
              },
            ],
          },
          {
            label: "实发合计",
            prop: "actualTotal",
            cell: true,
            rules: [
              {
                required: true,
                message: "请输入实发合计",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      data: [],
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: false,
        viewBtn: false,
        delBtn: this.vaildData(this.permission.wages_delete, false),
        editBtn: this.vaildData(this.permission.wages_edit, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  mounted() {
    this.initData();
  },
  methods: {
    handleExport() {
      if (this.query.accountPeriod) {
        window.open(
          `/api/blade-wages/wages/export-wages-bank?${
            this.website.tokenHeader
          }=${getToken()}&status=1&accountPeriod=${this.query.accountPeriod}`
        );
      } else {
        window.open(
          `/api/blade-wages/wages/export-wages-bank?${
            this.website.tokenHeader
          }=${getToken()}&status=1`
        );
      }
    },
    initData() {
      getDeptTree().then((res) => {
        const column = this.findObject(this.option.column, "deptId");
        column.dicData = res.data.data;
      });
    },
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      params.status = 1;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      )
        .then((res) => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
        })
        .catch(() => {
          this.data = [];
          this.loading = false;
        });
    },
  },
};
</script>

<style></style>
